﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace SpLocalizer.TranslationProvider.FileFormatProviders
{
    class ExcelFileTranslator : IFileTranslator
    {
        public string TranslateFile(AbstarctTranslationProvider provider, string SourceXml, System.Globalization.CultureInfo FromLanguage, System.Globalization.CultureInfo ToLanguage)
        {
            XmlDocument XmlDoc = new XmlDocument();
            XmlDoc.LoadXml(SourceXml);
            string str = "";
            System.Collections.ArrayList buf0 = new System.Collections.ArrayList();
            System.Collections.ArrayList buf1 = new System.Collections.ArrayList();
            System.Collections.ArrayList buf2 = new System.Collections.ArrayList();
            buf0.Add(buf1);
            buf0.Add(buf2);

            foreach (XmlNode Xnode in XmlDoc.SelectNodes("//*[local-name()='strings']"))
            {
                foreach (XmlNode XnodeS in Xnode.SelectNodes("//*[local-name()='t']"))
                {
                    str = provider.TranslateString(XnodeS.InnerXml, FromLanguage, ToLanguage, "text/plain");
                    (buf0[0] as System.Collections.ArrayList).Add(XnodeS.InnerXml);
                    (buf0[1] as System.Collections.ArrayList).Add(str);
                    XnodeS.InnerXml = str;
                }
            }
         /*   foreach (XmlNode Xnode in XmlDoc.SelectNodes("//*[local-name()='sheets']"))
            {

                for (int i = 0; i < (buf0[0] as System.Collections.ArrayList).Count; i++)
                {
                    Xnode.InnerXml = Xnode.InnerXml.Replace("[" + (buf0[0] as System.Collections.ArrayList)[i] + "]", "[" + (buf0[1] as System.Collections.ArrayList)[i] + "]");
                    Xnode.InnerXml = Xnode.InnerXml.Replace(">" + (buf0[0] as System.Collections.ArrayList)[i] + "<", ">" + (buf0[1] as System.Collections.ArrayList)[i] + "<");

                }
            } */

            foreach (XmlNode Xnode in XmlDoc.SelectNodes("//*[local-name()='sheets']"))
            {

                foreach (XmlNode XnodeS in Xnode.SelectNodes("//*[local-name()='v']") )
                {


                    if (XnodeS.InnerXml.IndexOf("[") == -1) { XnodeS.InnerXml = provider.TranslateString(XnodeS.InnerXml, FromLanguage, ToLanguage, "text/plain"); }
                  
                }

                for (int i = 0; i < (buf0[0] as System.Collections.ArrayList).Count; i++)
                {
                    Xnode.InnerXml = Xnode.InnerXml.Replace("[" + (buf0[0] as System.Collections.ArrayList)[i] + "]", "[" + (buf0[1] as System.Collections.ArrayList)[i] + "]");
                    Xnode.InnerXml = Xnode.InnerXml.Replace(">" + (buf0[0] as System.Collections.ArrayList)[i] + "<", ">" + (buf0[1] as System.Collections.ArrayList)[i] + "<");

                }
            }


            foreach (XmlNode Xnode in XmlDoc.SelectNodes("//*[local-name()='tables']"))
            {
                for (int i = 0; i < (buf0[0] as System.Collections.ArrayList).Count; i++)
                {
                    Xnode.InnerXml = Xnode.InnerXml.Replace("[" + (buf0[0] as System.Collections.ArrayList)[i] + "]", "[" + (buf0[1] as System.Collections.ArrayList)[i] + "]");
                    Xnode.InnerXml = Xnode.InnerXml.Replace(">" + (buf0[0] as System.Collections.ArrayList)[i] + "<", ">" + (buf0[1] as System.Collections.ArrayList)[i] + "<");
              
                }
            }
      /*      foreach (XmlNode Xnode in XmlDoc.SelectNodes("//*[local-name()='pivottables']"))
            {
                for (int i = 0; i < (buf0[0] as System.Collections.ArrayList).Count; i++)
                {
                    Xnode.InnerXml = Xnode.InnerXml.Replace("\"" + (buf0[0] as System.Collections.ArrayList)[i] + "\"", "[" + (buf0[1] as System.Collections.ArrayList)[i] + "\"");
                   // Xnode.InnerXml = Xnode.InnerXml.Replace(">" + (buf0[0] as System.Collections.ArrayList)[i] + "<", ">" + (buf0[1] as System.Collections.ArrayList)[i] + "<");

                }
            } */
            foreach (XmlNode Xnode in XmlDoc.SelectNodes("//*[local-name()='charts']"))
            {

                for (int i = 0; i < (buf0[0] as System.Collections.ArrayList).Count; i++)
                {
                    Xnode.InnerXml = Xnode.InnerXml.Replace("[" + (buf0[0] as System.Collections.ArrayList)[i] + "]", "[" + (buf0[1] as System.Collections.ArrayList)[i] + "]");
                    Xnode.InnerXml = Xnode.InnerXml.Replace(">" + (buf0[0] as System.Collections.ArrayList)[i] + "<", ">" + (buf0[1] as System.Collections.ArrayList)[i] + "<");

                }
            }

            return XmlDoc.InnerXml;
        }
    }
}
